import requests
import json

from urllib.parse import urlencode


def search_from_qq(value):
    url = 'http://u.y.qq.com/cgi-bin/musicu.fcg?'
    data = '{"comm":{"ct":11,"cv":"1003006","v":"1003006","QIMEI36":"","QIMEI":"892e0c96a0e17a8bd9bdeabcb61a86296150","os_ver":"12",\
            "phonetype":"Redmi K30 Pro","devicelevel":"31","tmeAppID":"qqmusiclight","nettype":"NETWORK_WIFI"},\
            "req":{"module":"music.search.SearchCgiService","method":"DoSearchForQQMusicLite",\
            "param":{"query":"周杰伦","search_type":0,"num_per_page":50,"page_num":1,"highlight":1,"nqc_flag":0,"page_id":1,"grp":1}}}'
    proxies = {'https': 'http://127.0.0.1:8888'}
    header = {'Content-Type': 'application/x-www-form-urlencoded',
              'User-Agent': 'Dalvik/2.1.0 (Linux; U; Android 7.1.2; SM-G9810 Build/QP1A.190711.020)'}

    urlencode_data = urlencode({'data':data})
    html = requests.get(url+urlencode_data, headers=header,proxies=proxies, verify=False)
    if html.status_code == 200:
        json_text = json.loads(html.text)
        with open('text.log', mode='w') as f:
            f.write(html.text)
        print(json_text)


def search_from_163(value):
    url = 'http://music.163.com/weapi/search/get'
    data = {
        'params': '5M/EpDowAIQUUXwhdj8oPyi9mzWIRYyRjoYfRDJol3GAIkTw5MjESm3I+L9+QOk2Q8PZZiQAAuCO3ItoncUAKTFQI7EXhJpRW8j9QuRewGvu9NGW6Osf9BY/jMo2FetR',
        'encSecKey': '257348aecb5e556c066de214e531faadd1c55d814f9be95fd06d6bff9f4c7a41f831f6394d5a3fd2e3881736d94a02ca919d952872e7d0a50ebfa1769a7a62d512f5f1ca21aec60bc3819a9c3ffca5eca9a0dba6d6f7249b06f5965ecfff3695b54e1c28f3f624750ed39e7de08fc8493242e26dbc4484a01c76f739e135637c'
    }
    proxies = {'https': 'http://127.0.0.1:8888'}
    header = {'Content-Type': 'application/x-www-form-urlencoded',
              'User-Agent': 'Dalvik/2.1.0 (Linux; U; Android 7.1.2; SM-G9810 Build/QP1A.190711.020)'}

    urlencode_data = urlencode(data)
    html = requests.post(url, data=urlencode_data, headers=header,proxies=proxies, verify=False)
    if html.status_code == 200:
        json_text = json.loads(html.text)
        with open('text163.log', mode='w') as f:
            f.write(html.text)
        print(json_text)


if __name__ == '__main__':
    search_from_163('周杰伦')